package com.henu.acmrating.config;

import com.henu.acmrating.interceptor.LoginINterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;


@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Value("${prop.upload-folder}")
    private String UPLOAD_FOLDER;



    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new LoginINterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(Arrays.asList("/api/login", "/api/register","/api/getAllBlog",
                        "/api/detailBlog", "/api/getUser","/api/showRank","/api/showRankByUser","/api/dynamic",
                        "/api/profile","/api/getCfScore","/api/weekStar","/api/getComments",
                        "/api/sendVerifyCode","/api/judgeVerifyCode",
                        "/api/modifyPassword","/api/crawler"));
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/api/img/**").addResourceLocations("file:" + UPLOAD_FOLDER);
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedHeaders("Content-Type","X-Requested-With","accept,Origin","Access-Control-Request-Method","Access-Control-Request-Headers","token")
                .allowedMethods("*")
                .allowedOrigins("*")
                .allowCredentials(true);
    }
}
